package com.game.framework.spring.factory;

import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.game.utils.log.Log;


/**
 * SpringBeanFactory
 * @author Liyuan
 */

public class SpringBeanFactory {
	/** CONFIG */
	private static String CONFIG = "context/context*.xml";
	/** CONTEXT */
	private static ApplicationContext CONTEXT = null;
	/** LOCALE */
	private static Locale LOCALE = new Locale("zh_CN");
	
	/** Private Constructors */
	private SpringBeanFactory() {
	} 
	
	/** init */
	public static synchronized void init(){
		try {
			if(CONTEXT == null){
				CONTEXT = new ClassPathXmlApplicationContext(CONFIG);
			}
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
	}
	
	/** reset */
	public static synchronized void reset(){
		try {
			CONTEXT = new ClassPathXmlApplicationContext(CONFIG);
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
	}
	
	/** getContext */
	public static synchronized ApplicationContext getContext(){
		if(CONTEXT == null) init();
		return CONTEXT;
	}
	
	/** setLocale */
	public static void setLocale(String language){
		LOCALE = new Locale(language);
	}
	
	/** getBean */
	public static Object getBean(String name){
		try {
			return CONTEXT.getBean(name);
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
		return null;
	}
	
	/** getMessage */
	public static String getMessage(String key){
		try {
			return CONTEXT.getMessage(key, null, LOCALE);
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
		return null;
	}
	
	/** getMessage */
	public static String getMessage(String key, Object... args){
		try {
			return CONTEXT.getMessage(key, args, LOCALE);
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
		return null;
	}
	
	/** getMessage */
	public static String getMessage(Locale locale, String key, Object... args){
		try {
			return CONTEXT.getMessage(key, args, locale);
		} catch (Exception e) {
			Log.printStackTrace(e);
		}
		return null;
	}
	
}
